package com.gti.mobilsafe.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationProvider {

	
	public static String getLocation(final Context context){
		
	
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		criteria.setCostAllowed(true);
		criteria.setAltitudeRequired(true);
		criteria.setSpeedRequired(true);
		
		String provider = locationManager.getBestProvider(criteria, true);
		
		locationManager.requestLocationUpdates(provider, 60000, 100, new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				double altitude = location.getAltitude();
				double longitude = location.getLongitude();
				float accuracy = location.getAccuracy();
				System.out.println("loaction " + altitude+" "+longitude+" "+accuracy);
				SharedPreferences sp = context.getSharedPreferences("location", Context.MODE_PRIVATE);
				Editor edit = sp.edit();
				edit.putString("location", altitude+" "+longitude+" "+accuracy);
				edit.commit();
				
			}
		});
		
		return provider;
	}

	
}
